package com.caineng.zhaobiao.zbapi.utils.redis;

import java.text.SimpleDateFormat;

/**
 * 序列编号类
 */
public class SequenceUtils {

    //默认长度
    static final int DEFAULT_LENGTH = 4 ;
    static SimpleDateFormat tempDate = new SimpleDateFormat("yyyyMMdd");

    /**
     * 传入每天的增长数字,如果位数不足补0
     * @param seq
     * @return
     */
    public static String getSequence(long seq) {
        String str = String.valueOf(seq);
        int len = str.length();
        if (len >= DEFAULT_LENGTH) {// 取决于业务规模,应该不会到达4
            return str;
        }
        int rest = DEFAULT_LENGTH - len;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < rest; i++) {
            sb.append('0');
        }
        sb.append(str);
        String datetime = tempDate.format(new java.util.Date());
        return datetime+sb.toString();
    }

    /**
     * 利用redis制作年月日序列号,这里生成redis的key
     * @return
     */
    public static String getSequenceKey(){

        String datetime = tempDate.format(new java.util.Date());
        return "CAINENG:WORK_CODE:SEQ:CODE:"+datetime;
    }
}
